Flash Article

Building a Google Search Application with Macromedia Flash MX Professional


Table of Contents

  1. Introduction
  2. Visual Programming in Macromedia Flash
  3. Authoring Animations
  4. Attaching Script Actions

Attaching Script Actions

Macromedia Flash MX 2004 features a new version of the application's native scripting language. ActionScript 2 is object-oriented and complies with ECMA-262 Edition 4. Still, visual programmers need to become familiar with the unique way that Flash attaches script actions to events in the application.

New behaviors make the process of triggering scripted UI component events much easier than in previous versions of Flash.

Here's how you use a behavior to trigger the web service and display its results:

  1. Click the btnSearch button on frmSearch. In the Behaviors panel, click the + icon and choose Data > Trigger Data Source. In the resulting dialog box, select GoogleSearch WebServiceConnector and click OK (see Figure 8).
  2. Figure 8. Binding a button event to the web service data source trigger in the Behaviors panel

    Figure 8. Binding a button event to the web service data source trigger in the Behaviors panel

  3. With btnSearch still selected, open the Actions panel. You'll see that Flash automatically added the code to trigger the web service when the user clicks the Search button.
  4. Any web service output you don't bind in the Component inspector needs to be managed with code. Just before the trigger event, add the code in Listing 2. These event listeners allow you to intercept the web service's send and result events and perform additional operations with ActionScript.
  5. on (click) {
    	
    	// Trigger Data Source Behavior
    	// Macromedia 2003
    	this._parent.GoogleSearch.addEventListener("send", onGoogleSearch);
    	this._parent.GoogleSearch.addEventListener("result", onGoogleSearchResult);
    	this._parent.GoogleSearch.trigger();
    
    }
    

    Listing 2. The btnSearch click event handler

  6. Add the ActionScript functions that handle these events. The code in Listing 3 does some interface work like enabling and disabling components while the search is in progress. Most importantly it iterates through the results returned from Google, formats them using HTML, and displays them in the txtResults TextArea instance. If you want to change the data or formatting, consult the Web Services panel to see the output parameter schema returned from the doGoogleSearch operation
  7. on (click) {
    	
    	var onGoogleSearch = function()
    	{
    		// Show the barber pole and disable search
    		_root.application.frmSearch.frmLoading.visible = true;
    		this._parent.txtResults.text = "";
    		this._parent.lblSearchTime.text = "";
    		this.enabled = false;
    		this._parent.txtQuery.enabled = false;
    	}
    	
    	var onGoogleSearchResult = function()
    	{
    		var res = this._parent.GoogleSearch.results.resultElements; // shorthand
    		for (var i = 0; i < res.length; i++) 
    		{
    			var s = "";
    			s += "<a href=\"" + res[i].URL + "\"><font size=\"+1\" color=\"#0000FF\"><u>" + res[i].title + "</u></font>>lt;/a><br>";
    			s += res[i].snippet + "<br>";
    			if (res[i].summary != "") { s += "<font color=\"#999999\">Description: </font>" + res[i].summary + "<br>"; }
    			s += "<a href=\"" + res[i].URL + "\"><font color=\"#009900\"><u>" + res[i].URL + "</u></font></a> - " + res[i].cachedSize;
    			s += "<br><br>";
    			this._parent.txtResults.text += s;
    		}
    
    		// Hide the barber pole and reenable the search
    		_root.application.frmSearch.frmLoading.visible = false;
    		this.enabled = true;
    		this._parent.txtQuery.enabled = true;
    	}
    	
    	// Trigger Data Source Behavior
    	// Macromedia 2003
    	this._parent.GoogleSearch.addEventListener("send", onGoogleSearch);
    	this._parent.GoogleSearch.addEventListener("result", onGoogleSearchResult);
    	this._parent.GoogleSearch.trigger();
    
    }
    

    Listing 3. The send and result event handlers for the Google web service

You're ready to publish your application. Publishing is the equivalent of building or compiling in Visual Studio and other IDEs. Select File > Publish or just press Control-Enter to publish using the default settings.

Now try out some Google searches!

Benefits of Flash

Let's review the benefits of Flash MX Professional as a visual programming environment:

  • More like the visual programming environment you know. There's no denying that Flash has a long heritage as a design and animation tool. With Flash MX Professional visual programmers can build visual applications that can—but don't need to—take advantage of the rich media capabilities of Flash. Forms, object-oriented ECMA-262–compliant ActionScript, components, and events make Flash programming more like VB programming than ever before. With unique rich media capabilities at your fingertips, Flash offers a lot more than traditional visual programming tools.
  • Rich applications. Even if you're not a designer, you can take advantage of easy-to-use Timeline effects and behaviors to add motion graphics to your form applications. Try doing this in Visual Basic!
  • True cross-platform, cross-device compatibility. Flash content can be viewed on almost any platform, including web, wireless, embedded devices, and (with Macromedia Central) on the desktop. Flash code is more reusable than Java code. You can move Flash applications among platforms without significant redesigning or recoding. For instance, converting the Google search application so it works on mobile devices or handhelds that support Flash Player 7 requires nothing more than resizing or rearranging the components on the Stage to fit a smaller screen size.

Macromedia Flash MX Professional is not a replacement for a Windows application IDE like Delphi or Visual Studio. However, visual programmers who want to author rich applications for deployment online, on handhelds, or on devices will discover familiar scenery in this new version of Flash.